package ch.bega.bhug.jass.helper;

import java.util.*;
import java.util.Map.Entry;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnKeyListener;
import android.widget.*;

public class MainHelper extends Activity {
    /** Called when the activity is first created. */

    static EditText EDIT_ANNONCE1;
    static EditText EDIT_ANNONCE2;
    static EditText EDIT_SCORE1;
    static EditText EDIT_SCORE2;

    static Button BUTTON_ANNONCE1;
    static Button BUTTON_ANNONCE2;
    static Button BUTTON_SCORE1;
    static Button BUTTON_SCORE2;
    
    static TextView TEXT_TOTAL1;
    static TextView TEXT_TOTAL2;
    
    static Map<EditText, Button> MAP_EDIT_BUTTON;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EDIT_ANNONCE1 = (EditText) findViewById(R.id.team1_annonces);
        EDIT_ANNONCE2 = (EditText) findViewById(R.id.team2_annonces);
        EDIT_SCORE1 = (EditText) findViewById(R.id.team1_score);
        EDIT_SCORE2 = (EditText) findViewById(R.id.team2_score);

        BUTTON_ANNONCE1 = (Button) findViewById(R.id.team1_addAnnoncesButton);
        BUTTON_ANNONCE2 = (Button) findViewById(R.id.team2_addAnnoncesButton);
        BUTTON_SCORE1 = (Button) findViewById(R.id.team1_addPointsButton);
        BUTTON_SCORE2 = (Button) findViewById(R.id.team2_addPointsButton);
        
        MAP_EDIT_BUTTON = new HashMap<EditText, Button>();
        MAP_EDIT_BUTTON.put(EDIT_ANNONCE1, BUTTON_ANNONCE1);
        MAP_EDIT_BUTTON.put(EDIT_ANNONCE2, BUTTON_ANNONCE2);
        MAP_EDIT_BUTTON.put(EDIT_SCORE1, BUTTON_SCORE1);
        MAP_EDIT_BUTTON.put(EDIT_SCORE2, BUTTON_SCORE2);
        

        TEXT_TOTAL1 = (TextView) findViewById(R.id.team1_total);
        TEXT_TOTAL2 = (TextView) findViewById(R.id.team2_total);
        
    }
    
    void setHandleEnter() {
        
        for(final Entry<EditText, Button> vEntry : MAP_EDIT_BUTTON.entrySet()){
            vEntry.getKey().setOnKeyListener(new OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {
                      // Perform action on key press
                        vEntry.getValue().performClick();
                      return true;
                    }
                    return false;
                }
            });
        }
    }
    
}